from rest_framework import serializers
from django.contrib.auth import get_user_model
from .models import UserPreference

User = get_user_model()

class UserSerializer(serializers.ModelSerializer):
    password = serializers.CharField(write_only=True)

    class Meta:
        model = User
        fields = ('id', 'username', 'email', 'password', 'phone', 
                 'organization', 'is_premium', 'created_at', 'updated_at')
        read_only_fields = ('id', 'created_at', 'updated_at', 'is_premium')

    def create(self, validated_data):
        user = User.objects.create_user(
            username=validated_data['username'],
            email=validated_data.get('email', ''),
            password=validated_data['password'],
            phone=validated_data.get('phone', ''),
            organization=validated_data.get('organization', '')
        )
        return user

class UserPreferenceSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserPreference
        fields = ('id', 'user', 'data_sources', 'visualization_settings', 
                 'notification_settings', 'created_at', 'updated_at')
        read_only_fields = ('id', 'user', 'created_at', 'updated_at') 